package cn.iocoder.yudao.module.of.service.taskinfo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.common.core.SysConstants;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
import cn.iocoder.yudao.module.of.controller.admin.taskhandler.vo.TaskHandlerRespVO;
import cn.iocoder.yudao.module.of.controller.admin.taskhandler.vo.TaskHandlerSaveReqVO;
import cn.iocoder.yudao.module.of.controller.admin.taskinfo.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.taskhandler.TaskHandlerDO;
import cn.iocoder.yudao.module.of.dal.dataobject.taskinfo.TaskInfoDO;
import cn.iocoder.yudao.module.of.dal.mysql.taskinfo.TaskInfoMapper;
import cn.iocoder.yudao.module.of.enums.HandlerTypeEnum;
import cn.iocoder.yudao.module.of.enums.TaskLevelEnum;
import cn.iocoder.yudao.module.of.enums.TaskStatusEnum;
import cn.iocoder.yudao.module.of.service.taskapproval.TaskApprovalService;
import cn.iocoder.yudao.module.of.service.taskhandler.TaskHandlerService;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptTreeVO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.UserDeptDO;
import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper;
import cn.iocoder.yudao.module.system.dal.mysql.user.UserDeptMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.of.enums.ErrorCodeConstantsOfTask.TASK_INFO_NOT_EXISTS;
import static cn.iocoder.yudao.module.of.enums.ErrorCodeConstantsOfTask.TASK_NOT_ALLOW_CREATE;

/**
 * 任务模块 Service 实现类
 *
 * @author 超级管理员
 */
@Slf4j
@Service
@Validated
public class TaskInfoServiceImpl implements TaskInfoService {

    @Resource
    private TaskInfoMapper taskInfoMapper;
    @Resource
    private DeptService deptService;
    @Resource
    private UserDeptMapper userDeptMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private AdminUserService adminUserService;
    @Resource
    private TaskHandlerService taskHandlerService;
    @Resource
    @Lazy
    private TaskApprovalService taskApprovalService;
    @Resource
    private RedisTemplate<String, Long> redisTemplate;
    @Resource
    private FileMapper fileMapper;

    private static final String ATOM_ID_KEY = "task:code:%s:";

    private static final String TASK_CODE_FORMAT = "SW%s%s";


    /**
     * 获得原子序列号
     *
     * @return 结果
     */
    public long getAtomId(String key) {
        // 原子递增操作
        Long result = redisTemplate.opsForValue().increment(key, 1L);

        // 设置过期时间
        redisTemplate.expire(key, Duration.ofDays(1));

        return result != null ? result : 0;
    }

    /**
     * 创建任务
     *
     * @param createReqVO 创建任务请求
     * @return 任务ID
     */
    @Transactional
    @Override
    public Long createTaskInfo(TaskInfoAddReqVO createReqVO) {
        TaskInfoDO taskInfo = BeanUtils.toBean(createReqVO, TaskInfoDO.class);
        if (createReqVO.getTaskMeeting() != null) {
            taskInfo.setMeetingId(createReqVO.getTaskMeeting().getId());
        }
        if (createReqVO.getTaskDocument() != null) {
            taskInfo.setDocumentId(createReqVO.getTaskDocument().getId());
        }
        taskInfoMapper.insert(taskInfo);

        // 处理任务办理人
        taskHandlerService.createTaskHandler(taskInfo.getId(), createReqVO.getTaskHandlerList());
        ifLeaderAssign(taskInfo.getId(), createReqVO.getTaskHandlerList());

        // 处理子任务
        if (CollectionUtils.isNotEmpty(createReqVO.getChildren())) {
            createReqVO.getChildren().forEach(taskInfoSaveReqVO -> {
                TaskInfoDO taskInfoOfChild = BeanUtils.toBean(taskInfoSaveReqVO, TaskInfoDO.class);
                taskInfoOfChild.setParentId(taskInfo.getId());
                taskInfoOfChild.setTaskFrom(taskInfo.getTaskFrom());
                taskInfoOfChild.setTaskType(taskInfo.getTaskType());
                taskInfoOfChild.setTaskLevel(TaskLevelEnum.SUB_LEVEL.getCode());

                taskInfoMapper.insert(taskInfoOfChild);
                // 处理任务办理人
                taskHandlerService.createTaskHandler(taskInfoOfChild.getId(), taskInfoSaveReqVO.getTaskHandlerList());
                ifLeaderAssign(taskInfoOfChild.getId(), taskInfoSaveReqVO.getTaskHandlerList());
            });
        }

        if (Objects.equals(taskInfo.getStatus(), TaskStatusEnum.PENDING.getStatus())) {
            submitApprovalTask(taskInfo.getId());
        }

        // 返回
        return taskInfo.getId();
    }

    @Transactional
    @Override
    public void updateTaskInfo(TaskInfoUpdateReqVO taskInfo) {
        // 校验存在
        validateTaskInfoExists(taskInfo.getId());
        // 更新
        TaskInfoDO updateObj = BeanUtils.toBean(taskInfo, TaskInfoDO.class);
        if (taskInfo.getTaskMeeting() != null) {
            updateObj.setMeetingId(taskInfo.getTaskMeeting().getId());
        }
        if (taskInfo.getTaskDocument() != null) {
            updateObj.setDocumentId(taskInfo.getTaskDocument().getId());
        }
        taskInfoMapper.updateById(updateObj);
        // 处理任务办理人
        taskHandlerService.updateTaskHandler(taskInfo.getId(), taskInfo.getTaskHandlerList());
        ifLeaderAssign(taskInfo.getId(), taskInfo.getTaskHandlerList());

        // 处理子任务
        if (CollectionUtils.isNotEmpty(taskInfo.getChildren())) {
            taskInfo.getChildren().forEach(taskInfoSaveReqVO -> {
                TaskInfoDO taskInfoOfChild = BeanUtils.toBean(taskInfoSaveReqVO, TaskInfoDO.class);
                taskInfoOfChild.setParentId(taskInfo.getId());
                taskInfoOfChild.setTaskFrom(taskInfo.getTaskFrom());
                taskInfoOfChild.setTaskType(taskInfo.getTaskType());
                taskInfoOfChild.setTaskLevel(TaskLevelEnum.SUB_LEVEL.getCode());

                taskInfoMapper.updateById(taskInfoOfChild);
                // 处理任务办理人
                taskHandlerService.updateTaskHandler(taskInfoOfChild.getId(), taskInfoSaveReqVO.getTaskHandlerList());
                ifLeaderAssign(taskInfoOfChild.getId(), taskInfoSaveReqVO.getTaskHandlerList());
            });
        }
        if (Objects.equals(taskInfo.getStatus(), TaskStatusEnum.PENDING.getStatus())) {
            submitApprovalTask(taskInfo.getId());
        }
    }

    /**
     * 是否集团分派
     */
    private void ifLeaderAssign(Long taskId, List<TaskHandlerSaveReqVO> taskHandlerList) {
        // 如果当前操作人租户和任务的租户不一致
        // 租户id
        Long tenantId = TenantContextHolder.getRequiredTenantId();

        taskHandlerList.forEach(handler -> {
            if (!tenantId.equals(handler.getTenantId())) {
                TaskInfoDO taskInfo = new TaskInfoDO();
                taskInfo.setId(taskId);
                taskInfo.setLeaderAssign(1);
                taskInfoMapper.updateById(taskInfo);
            }
            if (Objects.equals(handler.getHandlerType(), HandlerTypeEnum.MASTER_LEVEL.getCode())) {
                // 主办，设置租户
                TaskInfoDO taskInfo = new TaskInfoDO();
                taskInfo.setId(taskId);
                taskInfo.setTenantId(handler.getTenantId());
                taskInfoMapper.updateById(taskInfo);
            }
        });

    }

    @Transactional
    @Override
    public void deleteTaskInfo(Long id) {
        // 校验存在
        validateTaskInfoExists(id);
        // 删除
        taskInfoMapper.deleteById(id);
        taskHandlerService.deleteTaskHandler(id);

        // 删除子任务
        LambdaQueryWrapper<TaskInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskInfoDO::getParentId, id);
        List<TaskInfoDO> subTaskInfoList = taskInfoMapper.selectList(queryWrapper);
        subTaskInfoList.forEach(task -> {
            taskHandlerService.deleteTaskHandler(task.getId());
        });

        taskInfoMapper.delete(queryWrapper);
    }

    /**
     * 校验任务是否存在
     *
     * @param id 任务id
     */
    private void validateTaskInfoExists(Long id) {
        if (taskInfoMapper.selectById(id) == null) {
            throw exception(TASK_INFO_NOT_EXISTS);
        }
    }

    /**
     * 获得任务详情
     *
     * @param taskId 任务id
     * @return 任务模块
     */
    @Override
    public TaskInfoRespVO getTaskDetail(Long taskId) {
        // 获得数据来源项
        TaskInfoDO task = taskInfoMapper.selectById(taskId);

        return buildTaskDetailVO(task);
    }

    /**
     * 构建任务详情
     *
     * @param task 任务信息
     * @return
     */
    private TaskInfoRespVO buildTaskDetailVO(TaskInfoDO task) {
        TaskInfoRespVO vo = new TaskInfoRespVO();
        BeanUtils.copyProperties(task, vo);

        vo.setStatusName(TaskStatusEnum.valueOf(vo.getStatus(), task.getTaskLevel()));

        List<TaskHandlerRespVO> taskHandler = taskHandlerService.getTaskHandler(task.getId());
        vo.setTaskHandlerList(taskHandler);

        if (CollectionUtils.isNotEmpty(task.getTaskAttachment())) {
            List<FileDO> fileDOS = fileMapper.selectList(FileDO::getId, task.getTaskAttachment());
            vo.setTaskAttachmentList(fileDOS);
        }
        if (CollectionUtils.isNotEmpty(task.getTaskDocFile())) {
            List<FileDO> fileDOS = fileMapper.selectList(FileDO::getId, task.getTaskDocFile());
            vo.setTaskDocFileList(fileDOS);
        }
        return vo;
    }

    /**
     * 获得子任务详情
     *
     * @param masterTaskId 主任务id
     * @return 任务集合
     */
    @Override
    public List<TaskInfoRespVO> getSubTaskDetail(Long masterTaskId) {
        // 获得子任务
        LambdaQueryWrapper<TaskInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskInfoDO::getParentId, masterTaskId);
        List<TaskInfoDO> subTaskList = taskInfoMapper.selectList(queryWrapper);

        return subTaskList.stream().map(this::buildTaskDetailVO).collect(Collectors.toList());
    }


    /**
     * 获得任务分页
     *
     * @param req 分页查询
     * @return 任务分页
     */
    @Override
    public PageResult<TaskInfoRespVO> getTaskInfoPage(TaskInfoPageReqVO req) {
        List<Long> userIds = null; // 空，则说明时集团领导

        // 集团领导班子和任务管理员角色，都可以看到
        List<UserDeptDO> userDeptDOS = userDeptMapper.selectList(UserDeptDO::getUserId, WebFrameworkUtils.getLoginUserId());
        UserDeptDO JDDept = userDeptDOS.stream().filter(ud -> ud.getDeptId() == SysConstants.JT_LEADER_DEPT.longValue()).findFirst().orElse(null);
        List<UserRoleDO> userRoleDOS = userRoleMapper.selectListByUserId(WebFrameworkUtils.getLoginUserId());
        UserRoleDO taskAdminRole = userRoleDOS.stream().filter(ur -> ur.getRoleId().equals(SysConstants.TASK_ADMIN_ROLE_ID)).findFirst().orElse(null);

        if (JDDept == null && taskAdminRole == null) {
            // 非集团领导，则需要获得权限id
            userIds = deptService.getDeptOfUserIds();
        }

        MPJLambdaWrapperX<TaskInfoDO> queryWrapper = taskInfoMapper.getQueryWrapper();
        queryWrapper.distinct();
        queryWrapper.selectAll(TaskInfoDO.class);

        queryWrapper.eq(TaskInfoDO::getTaskLevel, TaskLevelEnum.MASTER_LEVEL.getCode());
        queryWrapper.likeIfPresent(TaskInfoDO::getTaskName, req.getTaskName());
        queryWrapper.eqIfPresent(TaskInfoDO::getTaskFrom, req.getTaskFrom());
        queryWrapper.eqIfPresent(TaskInfoDO::getTaskType, req.getTaskType());
        queryWrapper.eqIfPresent(TaskHandlerDO::getDeptId, req.getDeptId());
        queryWrapper.likeIfPresent(AdminUserDO::getNickname, req.getUserName());
        queryWrapper.eqIfPresent(TaskHandlerDO::getDeptId, req.getSubDeptId());
        queryWrapper.likeIfPresent(AdminUserDO::getNickname, req.getSubUserName());
        if (CollectionUtils.isNotEmpty(userIds)) {
            queryWrapper.in(TaskHandlerDO::getCreator, userIds);
        }
        if (StringUtils.isNotBlank(req.getMeetName())) {
            // 会议or公文中匹配
            queryWrapper.and(q -> q.like(TaskInfoDO::getTaskMeeting, req.getMeetName())
                    .or().like(TaskInfoDO::getTaskDocument, req.getMeetName()));
        }
        if (CollectionUtils.isNotEmpty(req.getStartTime()) && req.getStartTime().size() == 2) {
            queryWrapper.between(TaskInfoDO::getStartTime, req.getStartTime().get(0), req.getStartTime().get(1));
        }
        if (CollectionUtils.isNotEmpty(req.getEndTime()) && req.getEndTime().size() == 2) {
            queryWrapper.between(TaskInfoDO::getEndTime, req.getEndTime().get(0), req.getEndTime().get(1));
        }

        queryWrapper.innerJoin(TaskHandlerDO.class, TaskHandlerDO::getTaskId, TaskInfoDO::getId);
        queryWrapper.leftJoin(DeptDO.class, DeptDO::getId, TaskHandlerDO::getDeptId);
        queryWrapper.leftJoin(AdminUserDO.class, AdminUserDO::getId, TaskHandlerDO::getUserId);

        // 处理子任务查询条件
        if (req.getSubTaskDeptId() != null || req.getSubTaskSubDeptId() != null
                || StringUtils.isNotBlank(req.getSubTaskName()) || StringUtils.isNotBlank(req.getSubTaskUserName()) || StringUtils.isNotBlank(req.getSubTaskSubUserName())) {
            List<Long> ids = buildSubTaskQueryWrapper(req, userIds);
            if (CollectionUtils.isNotEmpty(ids)) {
                queryWrapper.in(TaskInfoDO::getId, ids);
            }
        }

        queryWrapper.orderByDesc(TaskInfoDO::getId);

        PageResult<TaskInfoDO> pageResult = taskInfoMapper.selectPage(req, queryWrapper);

        if (pageResult.getTotal() == 0) {
            return PageResult.empty();
        }

        // 构造具体结果
        List<TaskInfoRespVO> taskInfoRespVOS = buildPageResultList(pageResult.getList(), req, userIds);

        return new PageResult<>(taskInfoRespVOS, pageResult.getTotal());
    }

    /**
     * 构造子任务的查询条件
     *
     * @param req 查询条件
     */
    private List<Long> buildSubTaskQueryWrapper(TaskInfoPageReqVO req, List<Long> userIds) {
        MPJLambdaWrapperX<TaskInfoDO> queryWrapper = taskInfoMapper.getQueryWrapper();
        queryWrapper.distinct();
        queryWrapper.select(TaskInfoDO::getParentId);
        // 如果子查询条件是空，则不查询，只默认查询主任务
        queryWrapper.eq(TaskInfoDO::getTaskLevel, TaskLevelEnum.SUB_LEVEL.getCode());
        queryWrapper.likeIfPresent(TaskInfoDO::getTaskName, req.getSubTaskName());
        queryWrapper.eqIfPresent(TaskHandlerDO::getDeptId, req.getSubTaskDeptId());
        queryWrapper.eqIfPresent(TaskHandlerDO::getDeptId, req.getSubTaskSubDeptId());
        queryWrapper.likeIfPresent(AdminUserDO::getNickname, req.getSubTaskUserName());
        queryWrapper.likeIfPresent(AdminUserDO::getNickname, req.getSubTaskSubUserName());

        queryWrapper.leftJoin(TaskHandlerDO.class, TaskHandlerDO::getTaskId, TaskInfoDO::getId);
        queryWrapper.leftJoin(DeptDO.class, DeptDO::getId, TaskHandlerDO::getDeptId);
        queryWrapper.leftJoin(AdminUserDO.class, AdminUserDO::getId, TaskHandlerDO::getUserId);
        if (CollectionUtils.isNotEmpty(userIds)) {
            queryWrapper.in(TaskHandlerDO::getCreator, userIds);
        }

        return taskInfoMapper.selectList(queryWrapper).stream().map(TaskInfoDO::getParentId).collect(Collectors.toList());
    }

    private List<TaskInfoDO> buildSubTaskQueryWrapperOfResult(Long masterTaskId, TaskInfoPageReqVO req, List<Long> userIds) {
        MPJLambdaWrapperX<TaskInfoDO> queryWrapper = taskInfoMapper.getQueryWrapper();
        queryWrapper.distinct();
        queryWrapper.selectAll(TaskInfoDO.class);
        // 如果子查询条件是空，则不查询，只默认查询主任务
        queryWrapper.eq(TaskInfoDO::getParentId, masterTaskId);
        queryWrapper.eq(TaskInfoDO::getTaskLevel, TaskLevelEnum.SUB_LEVEL.getCode());
        queryWrapper.likeIfPresent(TaskInfoDO::getTaskName, req.getSubTaskName());
        queryWrapper.eqIfPresent(TaskHandlerDO::getDeptId, req.getSubTaskDeptId());
        queryWrapper.eqIfPresent(TaskHandlerDO::getDeptId, req.getSubTaskSubDeptId());
        queryWrapper.likeIfPresent(AdminUserDO::getNickname, req.getSubTaskUserName());
        queryWrapper.likeIfPresent(AdminUserDO::getNickname, req.getSubTaskSubUserName());

        queryWrapper.leftJoin(TaskHandlerDO.class, TaskHandlerDO::getTaskId, TaskInfoDO::getId);
        queryWrapper.leftJoin(DeptDO.class, DeptDO::getId, TaskHandlerDO::getDeptId);
        queryWrapper.leftJoin(AdminUserDO.class, AdminUserDO::getId, TaskHandlerDO::getUserId);
        if (CollectionUtils.isNotEmpty(userIds)) {
            queryWrapper.in(TaskHandlerDO::getCreator, userIds);
        }

        return taskInfoMapper.selectList(queryWrapper);
    }

    /**
     * 构造具体结果
     *
     * @param masterTaskInfoList 主任务列表
     * @param req                查询条件
     * @return 结果
     */
    private List<TaskInfoRespVO> buildPageResultList(List<TaskInfoDO> masterTaskInfoList, TaskInfoPageReqVO req, List<Long> userIds) {
        List<TaskInfoRespVO> list = new ArrayList<>();

        masterTaskInfoList.forEach(task -> {
            TaskInfoRespVO vo = buildTaskDetailVO(task);

            // 获得子任务
            List<TaskInfoDO> subTaskList = buildSubTaskQueryWrapperOfResult(task.getId(), req, userIds);
            List<TaskInfoRespVO> children = subTaskList.stream().map(this::buildTaskDetailVO).collect(Collectors.toList());

            vo.setChildren(children);
            list.add(vo);
        });

        return list;
    }

    /**
     * 获得部门树
     *
     * @return 部门树
     */
    @Override
    public List<DeptTreeVO> getDeptTree() {
        // 租户id
        //Long tenantId = TenantContextHolder.getRequiredTenantId();

        return deptService.getDeptTreeForOf();
    }

    /**
     * 获得主办任务的部门信息
     *
     * @return DeptTreeVO
     */
    @Override
    public List<TaskDeptTreeVO> getMasterTaskDept() {
        Long userId = WebFrameworkUtils.getLoginUserId();
        Long tenantId = TenantContextHolder.getRequiredTenantId();
        // 子公司不允许创建任务
        if (!tenantId.equals(SysConstants.TOP_TENANT_ID)) {
            log.error("子公司不允许创建任务");
            throw exception(TASK_NOT_ALLOW_CREATE);
        }

        DeptTreeVO userDeptVO = deptService.getUserDeptVO(userId);
        TaskDeptTreeVO vo = new TaskDeptTreeVO();
        BeanUtils.copyProperties(userDeptVO, vo);

        AdminUserDO usrInfo = adminUserService.getUserById(userId);

        // 集团的，创建时默认就是自己的部门，自己就是接口人
//        vo.setShowName(usrInfo.getNickname());
//        vo.setShowId(userId);

        // 自己就是接口人
        vo.setDistributionUserName(usrInfo.getNickname());
        vo.setDistributionUserId(userId);

        return Collections.singletonList(vo);
    }


    /**
     * 获得部门用户
     *
     * @param deptUserReqVO 部门用户查询条件
     * @return 部门用户
     */
    @Override
    public PageResult<SampleUserVO> getDeptUser(DeptUserReqVO deptUserReqVO) {

        UserPageReqVO userPageReqVO = new UserPageReqVO();
        BeanUtils.copyProperties(deptUserReqVO, userPageReqVO);
        userPageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());

        PageResult<AdminUserDO> pageResult = adminUserService.getUserPage2(userPageReqVO);

        List<SampleUserVO> userVO = pageResult.getList().stream().map(user -> {
            SampleUserVO sampleUserVO = new SampleUserVO();
            BeanUtils.copyProperties(user, sampleUserVO);
            return sampleUserVO;
        }).collect(Collectors.toList());

        return new PageResult<>(userVO, pageResult.getTotal());
    }

    /**
     * 获得任务编号
     *
     * @return 任务编号
     */
    @Override
    public String getTaskCode() {

        String dateFormat = DateUtil.format(new Date(), "yyyyMMdd");
        String cacheKey = String.format(ATOM_ID_KEY, dateFormat);
        String atomId = NumberUtil.decimalFormat("000", getAtomId(cacheKey));
        return String.format(TASK_CODE_FORMAT, dateFormat, atomId);
    }

    /**
     * 提交审批
     * 插入到审批表中，主任务审批，子任务同步审批
     */
    @Transactional
    @Override
    public void submitApprovalTask(Long masterTaskId) {
        // 校验存在
        validateTaskInfoExists(masterTaskId);

        taskInfoMapper.updateStatus(masterTaskId, TaskStatusEnum.PENDING, true);

        List<TaskInfoDO> taskInfoDOS = listTaskListByMasterId(masterTaskId);
        taskApprovalService.createTaskApproval(taskInfoDOS);
    }

    /**
     * 根据主任务id,获得任务列表
     *
     * @return 任务列表
     */
    @Override
    public List<TaskInfoDO> listTaskListByMasterId(Long masterId) {
        LambdaQueryWrapper<TaskInfoDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskInfoDO::getId, masterId).or().eq(TaskInfoDO::getParentId, masterId);

        return taskInfoMapper.selectList(wrapper);
    }

}